﻿using DataChangeTools.WebApi.IRepository;
using System.Reflection;
using static Dapper.SqlMapper;

namespace DataChangeTools.WebApi.Repository
{
    /// <summary>
    /// 基类
    /// </summary>
    public abstract class BasicsRepository : IBasicsRepository
    {
        /// <summary>
        /// 参数
        /// </summary>
        public Dictionary<bool, Dictionary<string, string>>? param = null;

        /// <summary>
        /// 当前操作dal对象
        /// </summary>
        public dynamic? thisDal = null;

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Add(dynamic? entity)
        {
            var thisDalType = thisDal?.GetType();
            MethodInfo method = thisDalType.GetMethod("Add");
            return method.Invoke(thisDal, new object[] { entity });
        }

        public virtual bool AddList(dynamic? entityList)
        {
            throw new NotImplementedException();
        }

        public virtual bool Delete(int id)
        {
            var thisDalType = thisDal?.GetType();
            MethodInfo method = thisDalType.GetMethod("Delete");
            return method.Invoke(thisDal, new object[] { id });
        }

        public virtual bool Deletes(dynamic[]? ids)
        {
            throw new NotImplementedException();
        }

        public virtual bool Edit(dynamic? entity)
        {
            var thisDalType = thisDal?.GetType();
            MethodInfo method = thisDalType.GetMethod("Edit");
            return method.Invoke(thisDal, new object[] { entity });
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public virtual dynamic GetList()
        {
            var thisDalType = thisDal?.GetType();
            MethodInfo method = thisDalType.GetMethod("GetList");
            return method.Invoke(thisDal, new object[] { });
        }

        public virtual dynamic GetListById(int id)
        {
            var thisDalType = thisDal?.GetType();
            MethodInfo method = thisDalType.GetMethod("GetListById");
            return method.Invoke(thisDal, new object[] { id });
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public virtual dynamic GetListByParam(Dictionary<bool, Dictionary<string, string>>? param)
        {
            var thisDalType = thisDal?.GetType();
            MethodInfo method = thisDalType.GetMethod("GetListByParam");
            return method.Invoke(thisDal, new object[] { param });
        }

        public virtual dynamic GetPageList(int index, int size)
        {
            throw new NotImplementedException();
        }
    }
}
